﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Text;
using System.Threading;

public partial class SystemManage_PlaceList : BasePage
{
    ISyPlaceHandler placeManager = new SyPlaceHandler();
    private static bool isUpdate = false;
    private static bool isDel = false;
    DataBase db = new DataBase();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!strPermission.Contains("1011"))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }
                else
                {
                    //判断用户有没有添加目的地权限
                    if (strPermission.Contains("1012"))
                    {
                        lblPlaceAdd.Visible = true;
                        lblImport.Visible = true;
                        Button2.Visible = true;
                    }
                    else
                    {
                        lblPlaceAdd.Visible = false;
                        lblImport.Visible = false;
                        Button2.Visible = false;
                    }
                    //判断用户有没有修改目的地权限
                    if (strPermission.Contains("1013"))
                    {
                        isUpdate = true;
                    }
                    else
                    {
                        isUpdate = false;
                    }
                    //判断用户有没有删除目的地权限
                    if (strPermission.Contains("1014"))
                    {
                        isDel = true;
                    }
                    else
                    {
                        isDel = false;
                    }

                }

                //  CheckPagePermission();//判断当前用户有没有添加删除更新的权限

            }

            ViewState["OrderBy"] = "PKID";
            ViewState["Accending"] = "asc";
            dataBind();
        }
    }

    /// <summary>
    /// 处理传入参数。。返回SQL条件和sqlparameter
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <param name="parms"></param>
    private void ProcessParms(ref string sqlstr, ref SqlParameter[] parms)
    {
        List<SqlParameter> list = new List<SqlParameter>();
        sqlstr = "";
        if (ViewState["Value"] != null)
        {
            if (ViewState["Name"].ToString().ToLower() == "district")
            {
                list.Add(new SqlParameter("@ColumnName", ViewState["Value"].ToString()));
                list.Add(new SqlParameter("@HKDistrict", ViewState["Value"].ToString()));
                list.Add(new SqlParameter("@ENGDistrict", ViewState["Value"].ToString()));
                sqlstr += " and (" + ViewState["Name"].ToString() + " like  @ColumnName or ENGDistrict like @ENGDistrict or HKDistrict like @HKDistrict)";
            }
            else if (ViewState["Name"].ToString().ToLower() == "name")
            {
                list.Add(new SqlParameter("@ColumnName", ViewState["Value"].ToString()));
                list.Add(new SqlParameter("@HKName", ViewState["Value"].ToString()));
                list.Add(new SqlParameter("@ENGName", ViewState["Value"].ToString()));
                sqlstr += " and (" + ViewState["Name"].ToString() + " like @ColumnName or ENGName like @ENGName or HKName like @HKName)";
            }

        }
        if (ViewState["Status"] != null)
        {
            list.Add(new SqlParameter("@Status", ViewState["Status"].ToString()));
            sqlstr += " and Status = @Status";
        }

        parms = list.ToArray();
    }

    private void dataBind()
    {
        string sqlStr = "SELECT * FROM Sy_Place where 1=1 ";
        SqlParameter[] parms = null;
        string clause = string.Empty;
        ProcessParms(ref clause, ref parms);
        sqlStr += clause;

        DataTable dt = placeManager.GetList(sqlStr, parms);
        DataView view = dt.DefaultView;
        view.Sort = ViewState["OrderBy"] + " " + ViewState["Accending"];
        gvPlaceList.DataSource = view;
        gvPlaceList.DataBind();
        if (dt != null && dt.Rows.Count > 0)
        {
            lblTotal.Text = dt.Rows.Count.ToString();
        }
        else
        {
            lblTotal.Text = "0";
        }


    }


    protected void gvPlaceList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvPlaceList.PageIndex = e.NewPageIndex;
        dataBind();

    }
    protected void gvPlaceList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            if (!placeManager.DeleteByID(int.Parse(e.CommandArgument.ToString())))
            {
                Jscript.AlertInAjax(Resources.Messages.DeleteFailed, UpdatePanel1);
            }
        }
        dataBind();

    }
    protected void gvPlaceList_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (e.SortExpression.ToLower() == ViewState["OrderBy"].ToString().ToLower())
        {
            if (ViewState["Accending"].ToString().ToLower() == "asc")
            {
                ViewState["Accending"] = "desc";
            }
            else
            {
                ViewState["Accending"] = "asc";
            }
        }
        else
        {
            ViewState["OrderBy"] = e.SortExpression;
        }

        dataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (txtName.Text != string.Empty)
        {
            ViewState["Value"] = txtName.Text.Trim();
            ViewState["Name"] = ddlColumnName.SelectedValue;
        }
        else
        {
            ViewState["Value"] = null;
            ViewState["Name"] = null;
        }    
        if (radYes.Checked)
        {
            ViewState["Status"] = "1";
        }
        else if (radNo.Checked)
        {
            ViewState["Status"] = "0";
        }
        else
        {
            ViewState["Status"] = null;
        }
        dataBind();
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvPlaceList.PageSize = int.Parse(ddlPages.SelectedValue);
        dataBind();
    }
    protected void gvPlaceList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            LinkButton btnDel = (LinkButton)e.Row.FindControl("lbtnDel");
            Label lblEdit = (Label)e.Row.FindControl("lblUpdate");

            if (isUpdate)
            {
                lblEdit.Visible = true;
            }
            else
            {
                lblEdit.Visible = false;
            }

            if (isDel)
            {
                btnDel.Visible = true;
            }
            else
            {
                btnDel.Visible = false;
            }

            Label lblDistrict = (Label)e.Row.FindControl("lblDistrict");
            Label lblHKDistrict = (Label)e.Row.FindControl("lblHKDistrict");
            Label lblENDistrict = (Label)e.Row.FindControl("lblENDistrict");
            Label lblName = (Label)e.Row.FindControl("lblName");
            Label lblHKName = (Label)e.Row.FindControl("lblHKName");
            Label lblENName = (Label)e.Row.FindControl("lblENName");

            if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("en"))
            {
                lblENDistrict.Visible = true;
                lblENName.Visible = true;
                lblDistrict.Visible = false;
                lblHKDistrict.Visible = false;
                lblName.Visible = false;
                lblHKName.Visible = false;
            }
            else if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("zh-hk"))
            {
                lblENDistrict.Visible = false;
                lblENName.Visible = false;
                lblDistrict.Visible = false;
                lblName.Visible = false;
                lblHKDistrict.Visible = true;
                lblHKName.Visible = true;
            }
            else
            {
                lblENDistrict.Visible = false;
                lblENName.Visible = false;
                lblDistrict.Visible = true;
                lblName.Visible = true;
                lblHKDistrict.Visible = false;
                lblHKName.Visible = false;
            }

        }
    }

    protected void lbtnGo_Click(object sender, EventArgs e)
    {
        TextBox txtPages = ((TextBox)gvPlaceList.BottomPagerRow.FindControl("txtPages"));
        try
        {
            int i = Convert.ToInt32(txtPages.Text) - 1;
            if (i >= 0 && i < gvPlaceList.PageCount)
            {
                this.gvPlaceList.PageIndex = i;
                //数据绑定
                dataBind();
            }
            else
            {
                Jscript.AlertInAjax(Resources.Messages.WrongPageNumInput, UpdatePanel1);
                return;
            }
        }
        catch
        {
            Jscript.AlertInAjax(Resources.Messages.WrongPageNumInput, UpdatePanel1);
        }
    }

    private DataTable Export()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("District(CN)");
        dt.Columns.Add("District(HK)");
        dt.Columns.Add("District(EN)");
        dt.Columns.Add("Place(CN)");
        dt.Columns.Add("Place(HK)");
        dt.Columns.Add("Place(EN)");      
        dt.Columns.Add("Status");
        dt.Columns.Add("Remark");

        string sqlStr = "SELECT * FROM Sy_Place where 1=1 ";
        SqlParameter[] parms = null;
        string clause = string.Empty;
        ProcessParms(ref clause, ref parms);
        sqlStr += clause;

        DataTable dtP = placeManager.GetList(sqlStr, parms);
        if (dtP != null && dtP.Rows.Count > 0)
        {
            for (int i = 0; i < dtP.Rows.Count; i++)
            {
                dt.Rows.Add(new string[] {
                     dtP.Rows[i]["District"].ToString(),
                       dtP.Rows[i]["HKDistrict"].ToString(),
                         dtP.Rows[i]["ENGDistrict"].ToString(),
                           dtP.Rows[i]["Name"].ToString(),
                             dtP.Rows[i]["HKName"].ToString(),
                               dtP.Rows[i]["ENGName"].ToString(),
                                 dtP.Rows[i]["Status"].ToString(),
                     dtP.Rows[i]["Remark"].ToString()
                      });
            }
        }
        return dt;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {

        BackupToExcel.ExportExcel(Export(), "Place");
    }
}
